package com.lzxuni.modules.pestdetails.controller;

import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.R;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.pestdetails.entity.Pest;
import com.lzxuni.modules.pestdetails.entity.PestYear;
import com.lzxuni.modules.pestdetails.service.PestAnalysisService;
import com.lzxuni.modules.soilmoisture.service.TblSoilMoistureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 虫情 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/LR_PestAnalysis/PestAnalysis")
public class PestAnalysisController extends BaseController{

    @Autowired
    private PestAnalysisService pestAnalysisService;
    @Autowired
    private TblSoilMoistureService tblSoilMoistureService;

    //虫情月分析页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_Pest/pestanalysis/index_month");
        return mv;
    }

    //虫情月分析Echar图
    @RequestMapping("/MonthEcharts")
    public Object getMothEcharts(Pest pest) {
        List<Pest> pestlist = pestAnalysisService.queryMonthList(pest);
        Map<String, List<Pest>> map = new HashMap<>();
        Map<String, String> mapIn = new HashMap<>();
        int i=0;
        for(Pest dm : pestlist){
            if(map.containsKey(dm.getType())){//map中存在此id，将数据存放当前key的map中
                map.get(dm.getType()).add(dm);
            }else{//map中不存在，新建key，用来存放数据
                List<Pest> tmpList = new ArrayList<>();
                tmpList.add(dm);
                map.put(dm.getType(), tmpList);
                mapIn.put(i+"", dm.getType());
            }
            i++;
        }

        Map map0=new HashMap<>();
        map0.put("date",map);
        map0.put("mapkey",mapIn);
        return R.ok().put("data", JSON.toJSONString(map0));
    }

    //虫情年分析页面
    @RequestMapping("/IndexYear")
    public ModelAndView IndexYear() {
        ModelAndView mv = new ModelAndView("/LR_Pest/pestanalysis/index_year");
        return mv;
    }

    //墒情年分析Echar图
    @RequestMapping("/YearEcharts")
    public Object getYearEcharts(PestYear pestYear) {
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
//        Date asd = new Date();
//        String shijian = sdf1.format(asd);
        List<PestYear> pestlist = pestAnalysisService.queryYearList(pestYear);
        Map<String, List<PestYear>> map = new HashMap<>();
        Map<String, String> mapIn = new HashMap<>();
        int i=0;
        for(PestYear dm : pestlist){
            if(map.containsKey(dm.getType())){//map中存在此id，将数据存放当前key的map中
                map.get(dm.getType()).add(dm);
            }else{//map中不存在，新建key，用来存放数据
                List<PestYear> tmpList = new ArrayList<>();
                tmpList.add(dm);
                map.put(dm.getType(), tmpList);
                mapIn.put(i+"", dm.getType());
            }
            i++;
        }

        Map map0=new HashMap<>();
        map0.put("date",map);
        map0.put("mapkey",mapIn);
        return R.ok().put("data", JSON.toJSONString(map0));
    }

}
